package com.ct.rbac.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ct.rbac.convert.SysMenuConvertDOToModel;
import com.ct.rbac.mapper.SysMenusMapper;
import com.ct.rbac.model.SysMenusModel;
import com.ct.rbac.orm.SysMenusDO;
import com.ct.rbac.repository.ISysMenusRespontory;
import com.ct.repository.commons.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-13
 */
@Service
public class SysMenusRespontoryImpl extends BaseServiceImpl<SysMenusMapper, SysMenusDO> implements ISysMenusRespontory {


    @Resource
    private SysMenusMapper sysMenusMapper;
    @Resource
    private SysMenuConvertDOToModel sysMenuConvertDOToModel;

    /**
     * 分页查询角色
     *
     * @param status
     * @param keyword
     * @return
     */
    @Override
    public List<SysMenusModel> findPageMenu(Integer status, String keyword) {
        LambdaQueryWrapper<SysMenusDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(status), SysMenusDO::getHidden, status);
        lambdaQueryWrapper.and(StringUtils.isNoneEmpty(keyword), c -> c.like(SysMenusDO::getTitle, keyword).or().like(SysMenusDO::getPath, keyword));
        lambdaQueryWrapper.orderByAsc(SysMenusDO::getSorted);
        List<SysMenusDO> menusDOList = this.list( lambdaQueryWrapper);
        List<SysMenusModel> menuList = sysMenuConvertDOToModel.convert(menusDOList);
        List<SysMenusModel> newMenuList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(menuList)) {
            // 这里就把菜单进行tree化
            for (SysMenusModel sysMenusModel : menuList) {
                if (sysMenusModel.getParentId() == 0L) {
                    newMenuList.add(getMenuTree(sysMenusModel, menuList));
                }
            }
        }
        return newMenuList;
    }

    /**
     * 查询菜单信息--租户使用
     * @return
     */
    @Override
    public List<SysMenusModel> findSystemPageMenu(Long roleId){
        List<SysMenusDO> menusDOList = this.baseMapper.findRoleMenus(roleId);
        List<SysMenusModel> menuList = sysMenuConvertDOToModel.convert(menusDOList);
        List<SysMenusModel> newMenuList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(menuList)) {
            // 这里就把菜单进行tree化
            for (SysMenusModel sysMenusModel : menuList) {
                if (sysMenusModel.getParentId() == 0L) {
                    newMenuList.add(getMenuTree(sysMenusModel, menuList));
                }
            }
        }
        return newMenuList;
    }

    /**
     * 根据用户角色查询菜单信息
     *
     * @param roleId
     * @return
     */
    @Override
    public List<SysMenusModel> findSysMenusList(Long roleId) {
        List<SysMenusDO> roleMenus = sysMenusMapper.findRoleMenus(roleId);
        List<SysMenusModel> menuList = sysMenuConvertDOToModel.convert(roleMenus);

        // 这里就把菜单进行tree化
        List<SysMenusModel> newMenuList = new ArrayList<>();
        for (SysMenusModel sysMenusModel : menuList) {
            if (sysMenusModel.getParentId() == 0L) {
                newMenuList.add(getMenuTree(sysMenusModel, menuList));
            }
        }
        return newMenuList;
    }

    /**
     * tree的方法
     *
     * @param result
     * @param allMenuList
     * @return
     */
    private SysMenusModel getMenuTree(SysMenusModel result, List<SysMenusModel> allMenuList) {
        for (SysMenusModel sysMenusModel : allMenuList) {
            if (Objects.equals(sysMenusModel.getParentId(), result.getId())) {
                if (result.getChildren() == null) {
                    result.setChildren(new ArrayList<>());
                }
                result.getChildren().add(getMenuTree(sysMenusModel, allMenuList));
            }
        }
        return result;
    }


}
